package com.atguigu.gmall.list.controller;


import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.product.client.common.result.Result;
import com.atguigu.gmall.product.client.model.list.Goods;
import com.atguigu.gmall.product.client.model.list.SearchParam;
import com.atguigu.gmall.product.client.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class listApiController {

    @Autowired   //高级客户端  查询快 性能高
    private RestHighLevelClient restHighLevelClient;
    @Autowired   //springDataES  spring官方高级客户端  对高级客户端的封装   性能不如高级客户端restHighLevelClient
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private ListService listService;


    //创建索引
    @GetMapping("/createIndex")
    public Result createIndex(){
        //1.创建索引  2.type类型 3.mapping 映射
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    //导入索引  根据skuId
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }

    //删除索引  根据skuId
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }
    //增加热度  评分
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }
    //开始搜索ES索引库
    @PostMapping("/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){
        //springDataES ES官方
        SearchResponseVo vo =listService.search(searchParam);
        return vo;
    }

}
